/**
 * 
 */

function Common(){}

Common.dialogIndex = 5000;

Common.getIndex = function(){
	return Common.dialogIndex ++;
};

Common.showDialog = function(ops){
	var type = ops.attr.type || 'tip';
	var defaultOps = {
		attr: {
			id: 'bootstrap_dialog_' + (new Date().getTime()) + '' + Math.floor(Math.random() * 9999 + 1),
			title: '提示',
			sureText: '确定',
			cancelText: '取消',
			confirmText: '确认',
			showSure: true,
			showCancel: false,
			showConfirm: false,
			showClose: true,
			showHeader: true,
			showFooter: true,
			disabledSure: false,
			sureColseDialog: false,
			type: 'tip' // tip ,popup
		},
		css: {
			width: type == 'tip' ? 420 : 650, 
			height: type == 'tip' ? 150 : 420, 
			top: 50, left: 0, right: 0, 'z-index': Common.getIndex()
		},
		event: {
			sure: function(wid){
				Common.hideDialog(wid);
			}, cancel: function(wid){
				Common.hideDialog(wid);
			}, confirm: function(wid){
				Common.hideDialog(wid);
			}, close: function(wid){
				Common.hideDialog(wid);
			}, loaded: function(wid){
			}
		},
		data: {
			content: '',
			url: 'no'
		}
	};
	ops.attr = $.extend(defaultOps.attr, ops.attr);
	ops.css = $.extend(defaultOps.css, ops.css);
	ops.event = $.extend(defaultOps.event, ops.event);
	ops.data = $.extend(defaultOps.data, ops.data);
	ops.css.top = (CommonUtil.windowHeight() - ops.css.height) / 2;;
	$('<div id="'+ops.attr.id+'_cover" class="modal-backdrop fade in"></div>').appendTo(document.body);
	
	var html = '<div class="modal fade in" id="'+ops.attr.id+'" style="display: block;" aria-hidden="false">';
	html += '<div class="modal-dialog">';
	html += '<div class="modal-content">';
	if(ops.attr.showHeader){
		html += '<div class="modal-header">';
		html += ops.attr.showClose ? '<button type="button" class="close">×</button>' : '';
		html += '<h4 class="modal-title">'+ops.attr.title+'</h4>';
		html += '</div>';
	}
	html += '<div class="modal-body">';
	html += '' + (ops.data.url == 'no' ? ops.data.content == 'no' ? '' : ops.data.content : '');
	html += '</div>';
	if(ops.attr.showFooter && (ops.attr.showSure || ops.attr.showCancel || ops.attr.showConfirm)){
		html += '<div class="modal-footer">';
		html += '<div class="dialog-wait hide"><i class="icon-spinner icon-spin orange bigger-125"></i></div>';
		html += ops.attr.showSure ? '<button class="btn btn-sm btn-primary sure" '+(ops.attr.disabledSure && 'disabled="true"')+'>'
				+ops.attr.sureText+'</button>' : '';
		html += ops.attr.showCancel ? '<button class="btn btn-sm btn-primary cancel">'+ops.attr.cancelText+'</button>' : '';
		html += ops.attr.showConfirm ? '<button class="btn btn-sm btn-danger confirm">'+ops.attr.confirmText+'</button>' : '';
		html += '</div>';
	}
	html += '</div>';
	html += '</div>';
	html += '</div>';
	var $html = $(html);
	$html.appendTo(document.body);
	$html.css({'top': -1});
	ops.data.url != 'no' && $html.find('div.modal-body').load(ops.data.url, function(e, s, r){
		ops.event.loaded && ops.event.loaded.call(window, ops.attr.id);
	});
	(ops.data.url == 'no' && ops.data.content != 'no') && ops.event.loaded && ops.event.loaded.call(window, ops.attr.id);
	
	$html.find('button.sure').bind('click', function(e){
		if(ops.event.sure){
			ops.event.sure.call(window, ops.attr.id);
			if(ops.attr.sureColseDialog){
				Common.hideDialog(ops.attr.id);
			}
		}
		
	});
	
	$html.find('button.cancel').bind('click', function(e){
		ops.event.cancel && ops.event.cancel.call(window, ops.attr.id);
	});
	
	$html.find('button.close').bind('click', function(e){
		ops.event.close && ops.event.close.call(window, ops.attr.id);
	});
	$html.find('.modal-dialog').css({width: ops.css.width + 20, height: ops.css.height});
	$html.find('div.modal-body').animate({
		height: (ops.css.height - (ops.attr.showHeader ? 56 : 0) - (ops.attr.showFooter ? 69 : 0) - 20),
		width: ops.css.width
	});
	//$html.find('div.modal-content').animate(ops.css);
	$html.animate({top: ops.css.top/2});
	indexPage.escBindObj.push(ops.attr.id);	
	return $html;
};

Common.showDialog.setTitle = function($dialog, title){
	$dialog.find('.modal-title').html(title);
};
	
Common.hideDialog = function(wid){
	wid = typeof(wid) == 'string' ? wid : wid.attr('id');
	$('#' + wid).remove();
	$('#' + wid+'_cover').remove();
	
	for(var i in indexPage.escBindObj){
		if(indexPage.escBindObj[i] == wid){
			indexPage.escBindObj[i] = null;
		}
	}
};


/**
 * $.ajax的再次封装，请求方式为POST
 * @param url 请求URL
 * @param param 请求参数
 * @param success 包含三个参数(data:后台返回数据，jsondata:data格式化为josn格式的数据，other:等同于本函数第5个参数)
 * @param error 包含四个参数(XMLHttpRequest, textStatus, errorThrown, other:等同于本函数第5个参数) 可为空
 * @param other 额外参数，可作为success与error中的参数使用,可为空
 * @param isAlpha 额外参数，是否需要透明背景,可为空
 * @param isAsync ajax操作同步还是异步，false为同步 ,true为异步
 * @param ishaveWait 是否需要显示等待条，默认为true:需要，false不需要
 */
Common.ajax = function(ops){
	if(ops == undefined && typeof(ops) != 'object'){
		new Error('参数不正确！');
		return;
	}
	ops = $.extend({
		isAlpha: true, isAsync: true, dataType: 'text'
	}, ops);
	var sd = new Date();
	var startTime = sd.getTime();
	var param = ops.param;
	var ajax_spendTime = 0;
	if(param != undefined && param != null && param.length > 2){
		param = param.substring(0,1) == '&' ? param.substring(1) : param;
	}
	//showWait(1, '', '', tips, isAlpha);
	$.ajax({
		type : 'POST',
		async: ops.isAsync,
		url  : ops.url,
		data : param,
		dataType : ops.dataType,
		success : function(data) {
			var ed = new Date();
			var endTime = ed.getTime();
			ajax_spendTime = (endTime - startTime) / 1000;
			//hiddenWait();
			if (data != undefined && data != null && data != '') {
				if(data == 'loginTimeout'){
					top.data.showLoginPanel();
					ops.error.call(window, null, null, null, ops.other);
					return;
				} else if(data == 'noPermissions'){
					alert('您没有此操作权限！');
					ops.error.call(window, null, null, null, ops.other);
					return;
				}
				if(ops.success != undefined && ops.success != null && typeof(ops.success) == 'function'){
					var jsondata = data;
					if(typeof(jsondata) == 'string'){
						try {
							jsondata = JSON.parse(data);
						} catch (e) {
							ops.error.call(window, "数据格式错误", null, null, ops.other);
						}
					}
					ops.success.call(window, data, jsondata, ops.other);
				}
			} else {
				if(ops.error != undefined && ops.error != null && typeof(ops.error) == 'function'){
					ops.error.call(window, null, null, null, ops.other);
				}
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			var ed = new Date();
			var endTime = ed.getTime();
			ajax_spendTime = (endTime - startTime) / 1000;
			//hiddenWait();
			if(ops.error != undefined && ops.error != null && typeof(ops.error) == 'function'){
				ops.error.call(window, XMLHttpRequest, textStatus, errorThrown, ops.other);
			}
		}
	});
};

Common.getErrorPanel = function(ops){
	var html = '<div class="alert alert-danger hide">';
		if(ops.showColse){
			html += '	<button type="button" class="close">';
			html += '		<i class="icon-remove"></i>';
			html += '	</button>';
		}
		html += '	<strong>';
		html += '	<i class="'+ops.icon+'"></i>';
		html += ops.title;
		html += '	</strong>';
		html += '	<span class="errorSpan">' + ops.info + '</span>';
		html += '</div>';
	return html;
};


Common.ThreadPool = function(ops){
	ops = $.extend({
		times: 0,
		interval: 1000,
		callfunc: function(i, e){}
	}, ops);
	var runTime = 0;
	var inters = window.setInterval(function(){
		if(ops.times > 0){
			ops.callfunc && ops.callfunc(inters, runTime);
			if(runTime >= ops.times){
				window.clearInterval(inters);
			}
		}
		runTime++;
	}, ops.interval);
};

Common.showInputErrorFlag = {};
Common.showInputError = function(obj){
	var $this = obj, id = obj;
	if(typeof(obj) == 'string'){
		$this = $('#' + obj);
		id = obj;
	} else {
		id = $this.attr('id') || 'jquery';
	}
	if($this == undefined || $this.length == 0){
		return;
	}
	console.log(id);
	if(Common.showInputErrorFlag[id] == undefined){
		Common.showInputErrorFlag[id] = true;
		var count = 0;
		var border = $this.css('border');
		//var back = $this.css('background');
		var run = function(){
			if(count%2 == 0){
				$this.css('border', '1px double red');
				//$this.css('background', '#F9CD9A');
			} else {
				$this.css('border', border);
				//$this.css('background', back);
			}
			if(count > 9){
				window.clearInterval(inter);
				$this.css('border', border);
				//$this.css('background', back);
				delete Common.showInputErrorFlag[id];
			}
			count++;
		};
		var inter = setInterval(run, 50);
	}
};

(function(){
	if(top.data == null) {
		top.data = {};
	}
	if(top.data.ctx == undefined){
		var url = location.href;
		url = url.substring(url.indexOf(":") + 3);
		var startIndex = url.indexOf("/") + 1;
		var endIndex = url.indexOf("/", startIndex);
		if(endIndex != -1){
			top.data.ctx = url.substring(startIndex - 1, endIndex);
		} else {
			top.data.ctx = '';
		}
	}
	console.info("ctx=", top.data.ctx);
})();

